package com.ourhours.message.config;

import com.ourhours.redismq.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.util.Pool;

import java.util.HashSet;
import java.util.Set;

/**
 * Description: RedisMQConfig
 *
 * @author bingqiang.lin
 * @version V1.0
 * @since 2018/4/8
 */
@Configuration
public class RedisMQConfig extends CachingConfigurerSupport{

    @Autowired
    private Environment env;

    @Bean(name = "messageTrunktaskExecutor")
    public ThreadPoolTaskExecutor getMessageTrunktaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        // 线程池维护线程的最少数量
        threadPoolTaskExecutor.setCorePoolSize(100);
        // 线程池维护线程所允许的空闲时间
        threadPoolTaskExecutor.setKeepAliveSeconds(30000);
        // 线程池维护线程的最大数量
        threadPoolTaskExecutor.setMaxPoolSize(1000);
        // 线程池所使用的缓冲队列
        threadPoolTaskExecutor.setQueueCapacity(1000);
        return threadPoolTaskExecutor;
    }


    @Bean
    public RedisUtil redisUtil() {
        //redis config 配置
        JedisPoolConfig config = new JedisPoolConfig();

        //控制一个pool最多有多少个状态为idle(空闲的)的JEDIS实例
        try {
            Integer maxIdle = Integer.parseInt(env.getProperty("spring.redis.pool.max-idle"));
            config.setMaxIdle(maxIdle);
        } catch (Exception e) {}

        //控制一个pool最少有多少个状态为idle(空闲的)的JEDIS实例
        try {
            Integer minIdle = Integer.parseInt(env.getProperty("spring.redis.pool.min-idle"));
            config.setMinIdle(minIdle);
        } catch (Exception e) {}
        //最大连接数，如果赋值为-1，则表示不限制；如果pool已经分配了maxActive个JEDIS实例，则此时pool的状态为exhausted(耗尽)。
        try {
            Integer maxTotal = Integer.parseInt(env.getProperty("spring.redis.pool.max-active"));
            config.setMaxTotal(maxTotal);
        } catch (Exception e) {}

        Integer maxWait = -1;
        try {
            maxWait = Integer.parseInt(env.getProperty("spring.redis.pool.max-wait"));
            config.setMaxWaitMillis(maxWait);
        } catch (Exception e) {}

        Integer timeout = 0;
        try {
            timeout = Integer.parseInt(env.getProperty("spring.redis.timeout"));
        } catch (Exception e) {}

        //sentinel节点，如果为空则为单机模式
        String sentinelNodes = env.getProperty("spring.redis.sentinel.nodes");
        //密码
        String password = env.getProperty("spring.redis.password");

        //连接池
        Pool<Jedis> pool;

        //单机模式
        if(StringUtils.isEmpty(sentinelNodes)) {
            String host = env.getProperty("spring.redis.host");
            Integer port = Integer.parseInt(env.getProperty("spring.redis.port"));

            if(StringUtils.isEmpty(password)) {
                pool = new JedisPool(config, host, port, timeout);
            }else {
                pool = new JedisPool(config,host,port,timeout,password);
            }
        }else {
            //sentinal模式
            String masterName = env.getProperty("spring.redis.sentinel.master");
            Set<String> sentinels = new HashSet<>();
            sentinels.add(sentinelNodes);
            if(StringUtils.isEmpty(password)) {
                pool  = new JedisSentinelPool(masterName, sentinels, config, timeout);
            }else {
                pool  = new JedisSentinelPool(masterName, sentinels, config, timeout, password);
            }
        }
        //构建redis util
        RedisUtil redisUtil = new RedisUtil(pool);
        return redisUtil;
    }

}
